
#include "MKE06Z4.h"
#include "system_MKE06Z4.h"
#include "mytype.h"


#define _SYSTEMSERVICE_MODULE_
#include "systemservice.h"

/*****************************************************************************//*!
   *
   * @brief NMI interrupt.   
   * @param[in]  none.
   * @return  none.
   *
   * @ Pass/ Fail criteria: none.
   *****************************************************************************/
void NMI_Handler()
{
	while(1);
}

/*****************************************************************************//*!
   *
   * @brief PMC interrupt.   
   * @param[in]  none.
   * @return  none.
   *
   * @ Pass/ Fail criteria: none.
   *****************************************************************************/
void PMC_IRQHandler()
{
	LowVoltageFlag = 1;
	PMC_DisableLVWInterrupt(PMC);
	NVIC_DisableIRQ(PMC_IRQn);
}

/*****************************************************************************//*!
   *
   * @brief system service init.   
   * @param[in]  none.
   * @return  none.
   *
   * @ Pass/ Fail criteria: none.
   *****************************************************************************/
void SystemServiceInit()
{
	uint16_t Cnt;
	
	SIM->PINSEL &= ~SIM_PINSEL_IRQPS_MASK;
	SIM->PINSEL |= (3 << SIM_PINSEL_IRQPS_SHIFT);
	
	SIM->SCGC   |= SIM_SCGC_IRQ_MASK;                               //power to IRQ 
	IRQ->SC      =  (1 << IRQ_SC_IRQPDD_SHIFT)  |
					(0 << IRQ_SC_IRQEDG_SHIFT)  |
					(1 << IRQ_SC_IRQPE_SHIFT)   |
					(1 << IRQ_SC_IRQIE_SHIFT)   |
					(0 << IRQ_SC_IRQMOD_SHIFT)  ;
	
	IRQ->SC      |=   IRQ_SC_IRQACK_MASK;
	
	
	NVIC_SetPriority(IRQ_IRQn, 0xFF);
	NVIC_ClearPendingIRQ(IRQ_IRQn);
	NVIC_EnableIRQ(IRQ_IRQn);
	
	
	PMC_SPMSC2 = 	(PmcLVDTrip_High << PMC_SPMSC2_LVDV_SHIFT) |   //lvd
					(PmcLVWTrip_Low  << PMC_SPMSC2_LVWV_SHIFT) ;
	PMC_SPMSC1 = 	0x6D ;
	
		
	
	Cnt = 0;
	while(Cnt < 100)                       //wait for voltage stable
	{
		PMC_ClrLVWFlag(PMC);
		if( PMC_GetLVWFlag(PMC) == 0)
			Cnt++;
		else
			Cnt = 0;
	}
	
	LowVoltageFlag = 0;
	NVIC_SetPriority(PMC_IRQn, 0xFF);
	NVIC_ClearPendingIRQ(PMC_IRQn);
	NVIC_EnableIRQ(PMC_IRQn);
}


